//
//  HMStatusCell.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMStatusCell.h"
#import "HMColvPicture.h"

@interface HMStatusCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iv_icon;
@property (weak, nonatomic) IBOutlet UILabel *lb_name;
@property (weak, nonatomic) IBOutlet UIImageView *lv_mbRank;
@property (weak, nonatomic) IBOutlet UIImageView *lv_avator;
@property (weak, nonatomic) IBOutlet UILabel *lb_time;
@property (weak, nonatomic) IBOutlet UILabel *lb_source;
@property (weak, nonatomic) IBOutlet UILabel *lb_text;

@property (weak, nonatomic) IBOutlet HMColvPicture *colV_picture;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *colv_pwidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *colv_pheight;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *colv_pflowLayout;
@property (weak, nonatomic) IBOutlet UILabel *lb_transmit;

@end

@implementation HMStatusCell

-(void)setStatusModel:(HMStatusViewModel *)statusModel{
    _statusModel = statusModel;
    
    [_iv_icon sd_setImageWithURL:statusModel.url];
//    _lb_name.text = statusModel.status.usermodel.name;
    _lb_name.text = statusModel.status.user.name;
    _lv_mbRank.image = statusModel.img_mbRank;
    _lv_avator.image = statusModel.verified_type;
    _lb_time.text = statusModel.showTime;
    _lb_source.text = statusModel.sourceP;
    _lb_text.text = statusModel.status.text;
    
    _lb_transmit.text = statusModel.status.retweeted_status.text;
//    _lb_transmit.text = statusModel.status.retweModel.text;
//    NSLog(@"+++++++=%@",statusModel.status.pic_urls);
    NSInteger count = statusModel.pictures.count;
    NSLog(@"%ld --- %@", count, statusModel.status.user.name);
//    NSLog(@"%@",statusModel.pictures);
    _colV_picture.pictures = statusModel.pictures;
//    if (count == 0) {
//        return;
//    }
//    NSLog(@"%zd",count);
    CGSize pSize = [self caculatePictureSizecount:count];
//    if (count == 1) {
//        _colv_pflowLayout.itemSize = pSize;
//    }
    _colv_pwidth.constant = pSize.width;
    _colv_pheight.constant = pSize.height;
    
    [_colV_picture layoutIfNeeded];
}
-(CGSize)caculatePictureSizecount:(NSInteger)count{
    CGFloat margin = 8;
    CGFloat imgmargin = 2;
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - margin * 2;
    CGFloat imgwidth = (maxWidth - 2 * imgmargin)/3;
//    _colv_pflowLayout.itemSize = CGSizeMake(imgwidth, imgwidth);
//    _colv_pflowLayout.minimumLineSpacing = imgmargin;
//    _colv_pflowLayout.minimumInteritemSpacing = imgmargin;
    
    if (count == 0) {
        return CGSizeMake(0, 0);
    }
//    if (count == 1) {
//        HMPictuerInfo *info = self.statusModel.status.pic_urls.firstObject;
//      UIImage *img =  [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:info.wap360];
//        CGSize imgSize = img.size;
//        return imgSize;
//    }
    if (count == 4) {
        CGFloat width = imgwidth * 2 + imgmargin;
        return CGSizeMake(width, width);
    }
    
    CGFloat row = (count-1) / 3 + 1;
    CGFloat width = imgwidth * row +(row - 1);
    return CGSizeMake(maxWidth, width);
}



- (void)awakeFromNib {
    [super awakeFromNib];
    self.lb_transmit.numberOfLines = 0;
    self.lb_transmit.preferredMaxLayoutWidth = ScreenWidth - 16;
    
    self.lb_text.preferredMaxLayoutWidth = ScreenWidth - 16;
    self.lb_text.numberOfLines = 0;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat margin = 8;
    CGFloat imgmargin = 2;
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - margin * 2;
    CGFloat imgwidth = (maxWidth - 2 * imgmargin)/3;
    _colv_pflowLayout.itemSize = CGSizeMake(imgwidth, imgwidth);
    _colv_pflowLayout.minimumLineSpacing = imgmargin;
    _colv_pflowLayout.minimumInteritemSpacing = imgmargin;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
